{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7.12 \u8bbf\u95ee\u95ed\u5305\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8981\u6269\u5c55\u51fd\u6570\u4e2d\u7684\u67d0\u4e2a\u95ed\u5305\uff0c\u5141\u8bb8\u5b83\u80fd\u8bbf\u95ee\u548c\u4fee\u6539\u51fd\u6570\u7684\u5185\u90e8\u53d8\u91cf\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u95ed\u5305\u7684\u5185\u90e8\u53d8\u91cf\u5bf9\u4e8e\u5916\u754c\u6765\u8bb2\u662f\u5b8c\u5168\u9690\u85cf\u7684\u3002\n\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u8bbf\u95ee\u51fd\u6570\u5e76\u5c06\u5176\u4f5c\u4e3a\u51fd\u6570\u5c5e\u6027\u7ed1\u5b9a\u5230\u95ed\u5305\u4e0a\u6765\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def sample():\n    n = 0\n    # Closure function\n    def func():\n        print('n=', n)\n\n    # Accessor methods for n\n    def get_n():\n        return n\n\n    def set_n(value):\n        nonlocal n\n        n = value\n\n    # Attach as function attributes\n    func.get_n = get_n\n    func.set_n = set_n\n    return func"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4f7f\u7528\u7684\u4f8b\u5b50:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = sample()\nf()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.set_n(10)\nf()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.get_n()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u8bf4\u660e\u6e05\u695a\u5b83\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6709\u4e24\u70b9\u9700\u8981\u89e3\u91ca\u4e00\u4e0b\u3002\u9996\u5148\uff0cnonlocal \u58f0\u660e\u53ef\u4ee5\u8ba9\u6211\u4eec\u7f16\u5199\u51fd\u6570\u6765\u4fee\u6539\u5185\u90e8\u53d8\u91cf\u7684\u503c\u3002\n\u5176\u6b21\uff0c\u51fd\u6570\u5c5e\u6027\u5141\u8bb8\u6211\u4eec\u7528\u4e00\u79cd\u5f88\u7b80\u5355\u7684\u65b9\u5f0f\u5c06\u8bbf\u95ee\u65b9\u6cd5\u7ed1\u5b9a\u5230\u95ed\u5305\u51fd\u6570\u4e0a\uff0c\u8fd9\u4e2a\u8ddf\u5b9e\u4f8b\u65b9\u6cd5\u5f88\u50cf(\u5c3d\u7ba1\u5e76\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u7c7b)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7684\u6269\u5c55\uff0c\u8ba9\u95ed\u5305\u6a21\u62df\u7c7b\u7684\u5b9e\u4f8b\u3002\u4f60\u8981\u505a\u7684\u4ec5\u4ec5\u662f\u590d\u5236\u4e0a\u9762\u7684\u5185\u90e8\u51fd\u6570\u5230\u4e00\u4e2a\u5b57\u5178\u5b9e\u4f8b\u4e2d\u5e76\u8fd4\u56de\u5b83\u5373\u53ef\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nclass ClosureInstance:\n    def __init__(self, locals=None):\n        if locals is None:\n            locals = sys._getframe(1).f_locals\n\n        # Update instance dictionary with callables\n        self.__dict__.update((key,value) for key, value in locals.items()\n                            if callable(value) )\n    # Redirect special methods\n    def __len__(self):\n        return self.__dict__['__len__']()\n\n# Example use\ndef Stack():\n    items = []\n    def push(item):\n        items.append(item)\n\n    def pop():\n        return items.pop()\n\n    def __len__():\n        return len(items)\n\n    return ClosureInstance()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\u6765\u6f14\u793a\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = Stack()\ns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.push(10)\ns.push(20)\ns.push('Hello')\nlen(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.pop()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.pop()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.pop()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u8da3\u7684\u662f\uff0c\u8fd9\u4e2a\u4ee3\u7801\u8fd0\u884c\u8d77\u6765\u4f1a\u6bd4\u4e00\u4e2a\u666e\u901a\u7684\u7c7b\u5b9a\u4e49\u8981\u5feb\u5f88\u591a\u3002\u4f60\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\u6d4b\u8bd5\u5b83\u8ddf\u4e00\u4e2a\u7c7b\u7684\u6027\u80fd\u5bf9\u6bd4\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Stack2:\n    def __init__(self):\n        self.items = []\n\n    def push(self, item):\n        self.items.append(item)\n\n    def pop(self):\n        return self.items.pop()\n\n    def __len__(self):\n        return len(self.items)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u8fd9\u6837\u505a\uff0c\u4f60\u4f1a\u5f97\u5230\u7c7b\u4f3c\u5982\u4e0b\u7684\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from timeit import timeit\n# Test involving closures\ns = Stack()\ntimeit('s.push(1);s.pop()', 'from __main__ import s')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Test involving a class\ns = Stack2()\ntimeit('s.push(1);s.pop()', 'from __main__ import s')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u663e\u793a\uff0c\u95ed\u5305\u7684\u65b9\u6848\u8fd0\u884c\u8d77\u6765\u8981\u5feb\u5927\u69828%\uff0c\u5927\u90e8\u5206\u539f\u56e0\u662f\u56e0\u4e3a\u5bf9\u5b9e\u4f8b\u53d8\u91cf\u7684\u7b80\u5316\u8bbf\u95ee\uff0c\n\u95ed\u5305\u66f4\u5feb\u662f\u56e0\u4e3a\u4e0d\u4f1a\u6d89\u53ca\u5230\u989d\u5916\u7684self\u53d8\u91cf\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Raymond Hettinger\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\u8bbe\u8ba1\u51fa\u4e86\u66f4\u52a0\u96be\u4ee5\u7406\u89e3\u7684\u6539\u8fdb\u65b9\u6848\u3002\u4e0d\u8fc7\uff0c\u4f60\u5f97\u8003\u8651\u4e0b\u662f\u5426\u771f\u7684\u9700\u8981\u5728\u4f60\u4ee3\u7801\u4e2d\u8fd9\u6837\u505a\uff0c\n\u800c\u4e14\u5b83\u53ea\u662f\u771f\u5b9e\u7c7b\u7684\u4e00\u4e2a\u5947\u602a\u7684\u66ff\u6362\u800c\u5df2\uff0c\u4f8b\u5982\uff0c\u7c7b\u7684\u4e3b\u8981\u7279\u6027\u5982\u7ee7\u627f\u3001\u5c5e\u6027\u3001\u63cf\u8ff0\u5668\u6216\u7c7b\u65b9\u6cd5\u90fd\u662f\u4e0d\u80fd\u7528\u7684\u3002\n\u5e76\u4e14\u4f60\u8981\u505a\u4e00\u4e9b\u5176\u4ed6\u7684\u5de5\u4f5c\u624d\u80fd\u8ba9\u4e00\u4e9b\u7279\u6b8a\u65b9\u6cd5\u751f\u6548(\u6bd4\u5982\u4e0a\u9762 ClosureInstance \u4e2d\u91cd\u5199\u8fc7\u7684 __len__() \u5b9e\u73b0\u3002)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u4f60\u53ef\u80fd\u8fd8\u4f1a\u8ba9\u5176\u4ed6\u9605\u8bfb\u4f60\u4ee3\u7801\u7684\u4eba\u611f\u5230\u7591\u60d1\uff0c\u4e3a\u4ec0\u4e48\u5b83\u770b\u8d77\u6765\u4e0d\u50cf\u4e00\u4e2a\u666e\u901a\u7684\u7c7b\u5b9a\u4e49\u5462\uff1f\n(\u5f53\u7136\uff0c\u4ed6\u4eec\u4e5f\u60f3\u77e5\u9053\u4e3a\u4ec0\u4e48\u5b83\u8fd0\u884c\u8d77\u6765\u4f1a\u66f4\u5feb)\u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8fd9\u5bf9\u4e8e\u600e\u6837\u8bbf\u95ee\u95ed\u5305\u7684\u5185\u90e8\u53d8\u91cf\u4e5f\u4e0d\u5931\u4e3a\u4e00\u4e2a\u6709\u8da3\u7684\u4f8b\u5b50\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u603b\u4f53\u4e0a\u8bb2\uff0c\u5728\u914d\u7f6e\u7684\u65f6\u5019\u7ed9\u95ed\u5305\u6dfb\u52a0\u65b9\u6cd5\u4f1a\u6709\u66f4\u591a\u7684\u5b9e\u7528\u529f\u80fd\uff0c\n\u6bd4\u5982\u4f60\u9700\u8981\u91cd\u7f6e\u5185\u90e8\u72b6\u6001\u3001\u5237\u65b0\u7f13\u51b2\u533a\u3001\u6e05\u9664\u7f13\u5b58\u6216\u5176\u4ed6\u7684\u53cd\u9988\u673a\u5236\u7684\u65f6\u5019\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}